﻿
using Admin.NET.Application.Entity;
using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Http;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.ChannelsLiveDashboardGetLiveListResponse.Types;
namespace Admin.NET.Application;

/// <summary>
/// 设备模板服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class DeviceTemplateInfoService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<DeviceTemplateInfo> _deviceTemplateInfoRep;
    private readonly SqlSugarRepository<DeviceSlaveTemplate> _slaveRep;

    public DeviceTemplateInfoService(SqlSugarRepository<DeviceTemplateInfo> deviceTemplateInfoRep, SqlSugarRepository<DeviceSlaveTemplate> slaveRep) 
    {
        _deviceTemplateInfoRep = deviceTemplateInfoRep;
        this._slaveRep = slaveRep;
    }


    /// <summary>
    /// 分页查询设备模板
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询设备模板")]
    public async Task<SqlSugarPagedList<DeviceTemplateInfoOutput>> Page(PageDeviceTemplateInfoInput input)
    {
		input.SearchKey = input.SearchKey?.Trim();
        var query = _deviceTemplateInfoRep.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>
                u.TemplateName.Contains(input.SearchKey)
            )
            .WhereIF(input.CusdeviceOfflineDecision>0, u => u.CusdeviceOfflineDecision == input.CusdeviceOfflineDecision)
            .WhereIF(!string.IsNullOrWhiteSpace(input.TemplateName), u => u.TemplateName.Contains(input.TemplateName.Trim()))
            .Select<DeviceTemplateInfoOutput>();
		return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加设备模板
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加设备模板")]
    public async Task<long> Add(AddDeviceTemplateInfoInput input)
    {
        var entity = input.Adapt<DeviceTemplateInfo>();
        await _deviceTemplateInfoRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除设备模板
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除设备模板")]
    public async Task Delete(DeleteDeviceTemplateInfoInput input)
    {
        var entity = await _deviceTemplateInfoRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _deviceTemplateInfoRep.FakeDeleteAsync(entity);   //假删除
        //await _deviceTemplateInfoRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新设备模板
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新设备模板")]
    public async Task Update(UpdateDeviceTemplateInfoInput input)
    {
        var entity = input.Adapt<DeviceTemplateInfo>();
        await _deviceTemplateInfoRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
        var deviceTemplateInfo = await _deviceTemplateInfoRep.Context.Queryable<DeviceTemplateInfo>()
            .Includes(x => x.DeviceSlaveTemplates)
            .FirstAsync(x => x.Id == input.Id);
        if (input.DeviceSlaveTemplates!=null)
        {
            foreach (var item in input.DeviceSlaveTemplates)
            {
                var slave = deviceTemplateInfo.DeviceSlaveTemplates.FirstOrDefault(u=>u.Id== item.Id);
                //修改模板名称并保存
                if (slave!=null&&item.SlaveName!=slave.SlaveName)
                {
                    slave.SlaveName = item.SlaveName;
                    await _slaveRep.AsUpdateable(slave).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
                }
            }
        }
       
    }

    /// <summary>
    /// 获取设备模板
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取设备模板")]
    public async Task<DeviceTemplateInfo> Detail([FromQuery] QueryByIdDeviceTemplateInfoInput input)
    {
        return await _deviceTemplateInfoRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取设备模板列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取设备模板列表")]
    public async Task<List<DeviceTemplateInfoOutput>> List([FromQuery] PageDeviceTemplateInfoInput input)
    {
        return await _deviceTemplateInfoRep.AsQueryable().Select<DeviceTemplateInfoOutput>().ToListAsync();
    }





}
